package com.reconstructme.toggle;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
import android.text.Editable;
import android.text.TextWatcher;

public class Settings extends Activity {

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        resotre_settings();
    }
    
    private void resotre_settings() {
    	
    	// add listener for IP change and store changes
    	EditText ip_edit = (EditText)findViewById(R.id.ip_edit);
        ip_edit.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable arg0) {} // do nothing
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {} // do nothing

			public void onTextChanged(CharSequence s, int start, int before, int count) {
				SharedPreferences settings = getSharedPreferences(ReconstructMe.PREFS_NAME, MODE_PRIVATE);
				SharedPreferences.Editor editor = settings.edit();
				editor.putString(ReconstructMe.IP_TAG, String.valueOf(s));
				editor.commit();
			}
        });
        
        // add listener for Port change and store changes
        EditText port_edit = (EditText)findViewById(R.id.port_edit);
        port_edit.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable arg0) {} // do nothing
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {} // do nothing

			public void onTextChanged(CharSequence s, int start, int before, int count) {
				SharedPreferences settings = getSharedPreferences(ReconstructMe.PREFS_NAME, MODE_PRIVATE);
				SharedPreferences.Editor editor = settings.edit();
				editor.putInt(ReconstructMe.PORT_TAG, Integer.valueOf(s.toString()));
				editor.commit();
			}
        });
        
        // add listener for Message change and store changes
        EditText message_edit = (EditText)findViewById(R.id.message_edit);
        message_edit.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable arg0) {} // do nothing
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {} // do nothing

			public void onTextChanged(CharSequence s, int start, int before, int count) {
				SharedPreferences settings = getSharedPreferences(ReconstructMe.PREFS_NAME, MODE_PRIVATE);
				SharedPreferences.Editor editor = settings.edit();
				editor.putString(ReconstructMe.MESSAGE_TAG, String.valueOf(s.toString()));
				editor.commit();
			}
        });
    	
    	// get settings
        SharedPreferences settings = getSharedPreferences(ReconstructMe.PREFS_NAME, MODE_PRIVATE);
        String ip_restored = settings.getString(ReconstructMe.IP_TAG, ReconstructMe.IP_DEFAULT);
        int port_restored = settings.getInt(ReconstructMe.PORT_TAG,	ReconstructMe.PORT_DEFAULT);
        String msg_restored = settings.getString(ReconstructMe.MESSAGE_TAG, ReconstructMe.MESSAGE_DEFAULT);
        
        // set settings
        ip_edit.setText(ip_restored);
        port_edit.setText(String.valueOf(port_restored));
        message_edit.setText(msg_restored);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_settings, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
